package models;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import play.db.jpa.Model;

@Entity
@Table(name = "Component_TaskTypes_Users", uniqueConstraints = { @UniqueConstraint(columnNames = {
		"reviewer_id", "tasktype_id", "component_id" }) })
public class ComponentTaskTypeUser extends Model {

	public boolean status;

	@ManyToOne
	@JoinColumn(name = "reviewer_id")
	public User reviewer;

	@ManyToOne
	@JoinColumn(name = "tasktype_id")
	public TaskType taskType;

	@ManyToOne
	@JoinColumn(name = "component_id")
	public Component component;

	// will be used if he will review a taskType for the whole project
	public ComponentTaskTypeUser(User reviewer, TaskType taskType) {
		this.reviewer = reviewer;
		this.taskType = taskType;
		this.status = false;
	}

	// will be used if he will review a taskType for a specific component
	public ComponentTaskTypeUser(User reviewer, TaskType taskType,
			Component component) {
		this.reviewer = reviewer;
		this.taskType = taskType;
		this.component = component;
		this.status = false;
	}

	/**
	 * Change the status of the request to be accepted. If the request is for
	 * the whole project, then all the requests by that same reviewer to review
	 * the same task type for a specific component will be canceled.
	 * 
	 * @param acceptedRequest
	 *            The request to be accepted.
	 * @return boolean
	 */
	public boolean acceptReviewRequestHelper() {
		this.status = true;
		this.save();
		if (this.component.name.equalsIgnoreCase("default component")) {
			this.taskType.deleteComponentReviewRequests((Long) this.reviewer
					.getEntityId(),
					(Long) this.component.project.getEntityId(),
					this.component.project.getAllComponentReviewRequests());
		}
		return true;
	}

}
